package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Date; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.lb.jwitter.util.Encrypt;

import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinHocVientblDKMHTaiKhoanHocVienInsertAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private String username; 
    public void setUsername(String username) {
       this.username = username;
 }

    public String getUsername() {
       return username;
 }

    private String password; 
    public void setPassword(String password) {
       this.password = password;
 }

    public String getPassword() {
       return password;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private TblThongTinHocVienService tblThongTinHocVienService; 

    private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien; 
    public TblDKMHTaiKhoanHocVien  getTblDKMHTaiKhoanHocVien() {
        return tblDKMHTaiKhoanHocVien;
 }

    private TblDKMHTaiKhoanHocVienService tblDKMHTaiKhoanHocVienService; 

	private int preInsert(Message msg, TblDKMHTaiKhoanHocVien obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHTaiKhoanHocVien obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_TaiKhoanHocVien", "them")){ 
        return "noprivilege"; 
    }
        tblDKMHTaiKhoanHocVienService = (TblDKMHTaiKhoanHocVienService) context.getBean("tbl_DKMH_TaiKhoanHocVienClient"); 
        tblThongTinHocVienService = (TblThongTinHocVienService) context.getBean("tbl_ThongTin_HocVienClient"); 

        msg = new Message(); 
        tblDKMHTaiKhoanHocVien = new TblDKMHTaiKhoanHocVien(); 
        tblDKMHTaiKhoanHocVien.setUsername(username); 
        tblDKMHTaiKhoanHocVien.setPassword(Encrypt.sha(password, "SHA-256")); 
        tblDKMHTaiKhoanHocVien.setHocVienId(tblThongTinHocVienService.gettblThongTinHocVien(new Long(hocVienId))); 
        preInsert(msg,tblDKMHTaiKhoanHocVien); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblDKMHTaiKhoanHocVienService.createtblDKMHTaiKhoanHocVien(tblDKMHTaiKhoanHocVien); 
           postInsert(msg, tblDKMHTaiKhoanHocVien); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblDKMHTaiKhoanHocVien.setId(new Long(id)); 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_TaiKhoanHocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
